<template>
  <el-dialog
    :title="editId ? '编辑规格' : '新增规格'"
    v-model="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="specsForm"
      :rules="rules"
      ref="specs-Form"
      label-width="100px"
    >
      <el-form-item label="规格名称" prop="specsname">
        <el-input
          v-model="specsForm.specsname"
          placeholder="请输入规格名称"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格属性" class="demo-attr">
        <el-row
          style="width: 100%"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <el-col :span="8">
            <el-input
              v-model="attrArr[index]"
              placeholder="请输入属性值"
            ></el-input
          ></el-col>
          <el-col :span="4"
            ><el-button type="primary" plain @click="addAttr" v-if="index === 0"
              >增加</el-button
            >
            <el-button type="danger" plain @click="delAttr(index)" v-else
              >删除</el-button
            ></el-col
          >
        </el-row>
      </el-form-item>
      <el-form-item label="规格状态" prop="status">
        <!-- <template>
          <el-radio v-model="specsForm.status" :label="1">启用</el-radio>
          <el-radio v-model="specsForm.status" :label="2"
            >禁用</el-radio
          ></template
        > -->
        <el-radio-group v-model="specsForm.status" class="ml-4">
          <el-radio :label="1" size="large">启用</el-radio>
          <el-radio :label="2" size="large">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitSpecs()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import useSpecStore from "../../stores/specs";

const specs = useSpecStore();

// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);

//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};

// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义菜单表单
let specsForm = reactive({
  specsname: "",
  attrs: "",
  status: 1,
});
// 规格属性数组
let attrArr = reactive([""]);
// 定义编辑id
let editId = ref("");

// 表单规则
const rules = reactive({
  sepecsname: {
    required: true,
    message: "请输入规格名",
    trigger: "blur",
  },
  attrs: {
    required: true,
    message: "请输入规格属性",
    trigger: "blur",
  },
  status: {
    required: true,
    message: "请选择规格状态",
    trigger: "blur",
  },
});

const addAttr = () => {
  //   console.log("console11111111111111111");
  // 限制
  if (attrArr.length >= 5) {
    ElMessage({
      type: "warning",
      message: "你最多只能添加五个属性",
    });
    return;
  }
  // 增加一个属性
  attrArr.push("");
};
const delAttr = (index) => {
  // 删除一个属性
  attrArr.splice(index, 1);
};

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  specsForm = reactive({
    specsname: "",
    attrs: "",
    status: 1,
  });
  attrArr = reactive([]);
  // 关闭弹窗
  dialogVisible.value = false;
};

// 提交表单
const submitSpecs = () => {
  specsForm.attrs = attrArr.join(",");
  //   console.log("specsForm", specsForm);
  if (editId.value) {
    specs.editSpecsAction({ ...specsForm, id: editId.value });
  } else {
    specs.addSpecsAction(specsForm);
  }
  resetForm();
};

// 接收父组件数据
const acceptValue = (value) => {
  //   console.log("value", value);
  editId.value = value.id;
  specsForm = reactive({ ...value });
  attrArr = reactive([...value.attrs]);
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped></style>
